package commands.user
{
	import cultu.common.message.UserCriteria;
	import cultu.common.message.UserScenario;
	import cultu.common.service.UserManager;
	
	import model.Model;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import vo.UserVO;
	
	import wizeoo.commands.WIZRemoteCall;
	
	public class LoginCmd extends WIZRemoteCall
	{
		/**l'utilisateur qu'on recherche*/
		private var user:UserVO;
		
		/**Référence au model*/
		private var mod:Model = Model.getInstance();
		
		public function LoginCmd(pUser:UserVO,pCallback:Function=null)
		{
			super(pCallback);
			user=pUser;
		}
		
		override public function wizExecute():void
		{
			var um:UserManager = new UserManager();
			var us:UserScenario = new UserScenario();
			
			um.addEventListener(ResultEvent.RESULT,result,false,0,true);
			um.addEventListener(FaultEvent.FAULT,fault,false,0,true);
			
			um.getUser(null,us,user.srcUser);
		}
		
		override protected function wizResult(data:Object):void
		{
			if(data && data.result)
			{
				var usr:UserVO = new UserVO(data.result);
				
				if(usr.login == user.login && usr.password == user.password)
				{
					mod.connectedUser = usr;
				}
			}
		}
		
		override protected function wizFault(info:Object):void
		{
			trace('unable to log user');	
		}
	}
}